/*********************************************************************************
 *      Copyright:  (C) 2022 xiezongyi<1764417007@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  ssl.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(12/08/22)
 *         Author:  xiezongyi <1764417007@qq.com>
 *      ChangeLog:  1, Release initial version on "12/08/22 16:28:51"
 *                 
 ********************************************************************************/
#include <stdio.h> 
#include <stddef.h>

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>

#define SERVER_CRT  "server.crt"
#define SERVER_KEY  "server.key"

SSL_CTX *evssl_init(void)
{

    SSL_load_error_strings();
    SSL_library_init();

    if(!RAND_poll())
    {
        return NULL;
    }

    SSL_CTX *server_ctx = SSL_CTX_new(SSLv23_server_method());//建一个SSL_CTX *实例，用来保存证书的私钥。

    if(!SSL_CTX_use_certificate_chain_file(server_ctx, SERVER_CRT) || !SSL_CTX_use_PrivateKey_file(server_ctx, SERVER_KEY,    SSL_FILETYPE_PEM))
    {
        puts("Couldn't read 'server.key' or 'server.crt' file.  To generate a key\n"
           "and self-signed certificate, run:\n"
           "  openssl genrsa -out server.key\n"
           "  openssl req -new -key server.key -out server.req\n"
           "  openssl x509 -req -days 365 -in server.req -signkey server.key -out server.crt");

        return NULL;
    }

    SSL_CTX_set_options(server_ctx, SSL_OP_NO_SSLv2);

    return server_ctx;
}

